// zagSNES
// © MIT ∞ IamRifki

const std = @import("std");

/// Memory
const ram: *u8 = undefined;
const rom: *u8 = undefined;
const memLookup: [2048]u8 = undefined;
const memRead: [2048]u8 = undefined;
const memWrite: [2048]u8 = undefined;
const accessSpeed: [2048]u8 = undefined;

const loROM: i16 = undefined;

pub fn readMem(a: u32) u8 {
    cycles -= accessSpeed[(a >> 13) & 0x7FF];
    clockSpc(accessSpeed[(a >> 13) & 0x7FF]);
    if (memRead[(a >> 13) & 0x7FF]) {
        return memLookup[(a >> 13) & 0x7FF][a & 0x1FFF];
    }
    return readMeml(a);
}

pub fn writeMem(ad: u32, v: u8) void {
    cycles -= accessSpeed[(ad >> 13) & 0x7FF];
    clockSpc(accessSpeed[(ad >> 13) & 0x7FF]);
    if (memWrite[(ad >> 13) & 0x7FF]) {
        memLookup[(ad >> 13) & 0x7FF][(ad) & 0x1FFF] = v;
    } else {
        writeMeml(ad, v);
    }
}

pub fn readMemW(a: u32) u32 {
    const lo = readMem(a);
    const hi = readMem(a + 1);
    return lo | (hi << 8);
}

pub fn writeMemW(a: u32, v: u32) void {
    writeMem(a, v & 0xFF);
    writeMem(a + 1, v >> 8);
}

pub fn writeMemW2(a: u32, v: u32) void {
    writeMem(a + 1, v >> 8);
    writeMem(a, v & 0xFF);
}
